
package com.moogene.android;

import com.moogene.android.engine.action.ActionManager;
import com.moogene.android.engine.action.KeyAnalyse;
import com.moogene.android.engine.action.TouchAnalyse;
import com.moogene.android.engine.mmi.Book;
import com.moogene.android.engine.mmi.Page;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class MooBookView extends MooView {

    private static Book sBook;

    private static TouchAnalyse mMotionAnalyse;

    private static KeyAnalyse mKeyAnalyse;

    public MooBookView(Context context, AttributeSet attrs) {
        super(context, attrs);
        sBook = Book.getInstance();

        ActionManager actionManager = ActionManager.getInstance();
        mMotionAnalyse = actionManager.getTouchAnalyse();
        mKeyAnalyse = actionManager.getKeyAnalyse();
        actionManager.setActionListener(sBook);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
//            Page startPage = (Page)Class.forName(Config.FIRST_PAGE_CLASS_NAME).newInstance();
            sBook.gotoPage(Config.FIRST_PAGE_CLASS_NAME,true);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        startThread();
        System.out.println("Johney:surfaceCreated");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //		Rect r = sSurfaceHolder.getSurfaceFrame();
        super.surfaceChanged(holder, format, width, height);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent me) {
    	boolean hasHandled = mMotionAnalyse.onTouch(me);
//    	ActionManager.getInstance().update();
        if (!hasHandled) {
            return super.dispatchTouchEvent(me);
        }
        try {
            Thread.sleep(Config.TOUCH_INTERVAL);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        requestFocus();
        // invalidate();
        return true;
    }

    /**
     * Standard override for key-up.
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent msg) {
        return mKeyAnalyse.onKeyUp(keyCode);
    }

    /**
     * Standard override for key-up.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {
        return mKeyAnalyse.onKeyDown(keyCode);
    }

    private static long sFrameCount;

    private static long sTicker;

    public final static void startThread() {
        new Thread() {
            long tick;

            @Override
            public void run() {
                while (true) {
                    if (Config.DEBUG_FPS) {
                        if (System.currentTimeMillis()
                                - sTicker > 1000) {
                            Log.d(Config.DEBUG_FPS_TAG, sFrameCount
                                    + "");
                            sTicker = System.currentTimeMillis();
                            sFrameCount = 0;
                        } else {
                            sFrameCount++;
                        }
                    }
                    tick = System.currentTimeMillis()
                            + Config.FRAME_INTERVAL;
                    ActionManager.getInstance().update();
                    sBook.update();
                    Canvas c = null;
                    try {
                        c = sSurfaceHolder.lockCanvas(null);
                        synchronized (sSurfaceHolder) {
                            sBook.draw(c);
                        }
                    } finally {
                        // do this in a finally so that if an exception is thrown
                        // during the above, we don't leave the Surface in an
                        // inconsistent state
                        if (c != null) {
                            sSurfaceHolder.unlockCanvasAndPost(c);
                        }
                    }
                    while (System.currentTimeMillis() < tick) {
                        Thread.yield();
                    }
                }
            }
        }.start();
    }

}
